package cn.com.guage.dtm.mapping.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import cn.com.guage.dtm.common.utils.DateUtils;
import cn.com.guage.dtm.utils.VelocityInitializer;
import cn.com.guage.dtm.utils.VelocityUtils;

import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.guage.dtm.mapping.mapper.MappingTransTaskMapper;
import cn.com.guage.dtm.mapping.domain.MappingTransTask;
import cn.com.guage.dtm.mapping.service.IMappingTransTaskService;
import cn.com.guage.dtm.common.constant.Constants;
import cn.com.guage.dtm.common.core.text.Convert;

/**
 * 迁移作业管理Service业务层处理
 * 
 * @author liujun
 * @date 2021-07-14
 */
@Service
public class MappingTransTaskServiceImpl implements IMappingTransTaskService 
{
	private final static Logger log = LoggerFactory.getLogger(MappingTransTaskServiceImpl.class);
	
    @Autowired
    private MappingTransTaskMapper mappingTransTaskMapper;

    /**
     * 查询迁移作业管理
     * 
     * @param taskId 迁移作业管理ID
     * @return 迁移作业管理
     */
    @Override
    public MappingTransTask selectMappingTransTaskById(Long taskId)
    {
        return mappingTransTaskMapper.selectMappingTransTaskById(taskId);
    }

    /**
     * 查询迁移作业管理列表
     * 
     * @param mappingTransTask 迁移作业管理
     * @return 迁移作业管理
     */
    @Override
    public List<MappingTransTask> selectMappingTransTaskList(MappingTransTask mappingTransTask)
    {
        return mappingTransTaskMapper.selectMappingTransTaskList(mappingTransTask);
    }

    /**
     * 新增迁移作业管理
     * 
     * @param mappingTransTask 迁移作业管理
     * @return 结果
     */
    @Override
    public int insertMappingTransTask(MappingTransTask mappingTransTask)
    {
        mappingTransTask.setCreateTime(DateUtils.getNowDate());
        return mappingTransTaskMapper.insertMappingTransTask(mappingTransTask);
    }

    /**
     * 修改迁移作业管理
     * 
     * @param mappingTransTask 迁移作业管理
     * @return 结果
     */
    @Override
    public int updateMappingTransTask(MappingTransTask mappingTransTask)
    {
        mappingTransTask.setUpdateTime(DateUtils.getNowDate());
        return mappingTransTaskMapper.updateMappingTransTask(mappingTransTask);
    }

    /**
     * 删除迁移作业管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMappingTransTaskByIds(String ids)
    {
        return mappingTransTaskMapper.deleteMappingTransTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除迁移作业管理信息
     * 
     * @param taskId 迁移作业管理ID
     * @return 结果
     */
    @Override
    public int deleteMappingTransTaskById(Long taskId)
    {
        return mappingTransTaskMapper.deleteMappingTransTaskById(taskId);
    }

	@Override
	public byte[] downloadCode(Long taskId) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        generatorCode(taskId, zip);
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
	}
	
	/*
    * 查询表信息并生成代码
    */
   private void generatorCode(Long taskId, ZipOutputStream zip)
   {
       // 查询表信息
       MappingTransTask task = mappingTransTaskMapper.selectMappingTransTaskById(taskId);
       // 设置主子表信息

       VelocityInitializer.initVelocity();

       VelocityContext context = VelocityUtils.prepareContext(task);

       // 获取模板列表
       List<String> templates = VelocityUtils.getTransJobTemplateList();
       for (String template : templates)
       {
           // 渲染模板
           StringWriter sw = new StringWriter();
           Template tpl = Velocity.getTemplate(template, Constants.UTF8);
           tpl.merge(context, sw);
           try
           {
               // 添加到zip
               zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, task)));
               IOUtils.write(sw.toString(), zip, Constants.UTF8);
               IOUtils.closeQuietly(sw);
               zip.flush();
               zip.closeEntry();
           }
           catch (IOException e)
           {
               log.error("渲染模板失败，表名：" + task.getTableName(), e);
           }
       }
   }
}
